﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QLNSWPF2.ToolSupport;
using System.Data;

namespace QLNSWPF2
{
	/// <summary>
	/// Interaction logic for fmNhapNhanSu.xaml
	/// </summary>
	public partial class fmNhapNhanSu : Window
	{
        DataConnection dataConnection;
        public fmNhapNhanSu()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
		}

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dataConnection = new DataConnection();
            DataTable ethnic = dataConnection.ExecuteDataTableAll("select * from Ethnic", CommandType.Text);
            cbEthnic.DataContext = ethnic;

            DataTable Qualification = dataConnection.ExecuteDataTableAll("select * from Qualifications", CommandType.Text);
            cbQualifications.DataContext = Qualification;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {            
            string name = txtName.Text.Trim();
            
            bool gender = false;
            if (rMale.IsChecked == true)
                gender = true;
            else
                gender = false;
            string homeTown = txtHomeTown.Text.Trim();
            string ethnicId = dataConnection.ExecuteScalar("select EthnicId from Ethnic where EthnicName=N'" + cbEthnic.Text + "'", CommandType.Text).ToString();
            bool party = false;
            if (rPartyYes.IsChecked == true)
                party = true;
            else
                party = false;
            string title = cbTitle.Text;
            string stateTitle = cbStateTitle.Text;
            string qualifications = dataConnection.ExecuteScalar("select QualificationId from Qualifications where Qualification=N'" + cbQualifications.Text + "'", CommandType.Text).ToString();
            if (name != "" && dtmBirthday.SelectedDate != null && homeTown != "")
            {
                DateTime birthday = dtmBirthday.SelectedDate.Value;
                string query = String.Format("insert into Employees(Name, Birthday, Gender, PartyMember, EthnicId, Titles, StateTitle, HomeTown)"
                    + "values(N'{0}', '{1}', '{2}', '{3}', {4}, N'{5}', N'{6}', N'{7}')", name, birthday, gender, party, ethnicId, title, stateTitle, homeTown);
                dataConnection.ExecuteNonQuery(query, CommandType.Text);
                MessageBox.Show("Bạn đã thêm thành công!", "Thông báo");
                txtName.Text = "";
                rMale.IsChecked = true;
                txtHomeTown.Text = "";
                cbEthnic.SelectedIndex = 0;
                rPartyYes.IsChecked = true;
                cbTitle.SelectedIndex = 0;
                cbStateTitle.SelectedIndex = 0;
                cbQualifications.SelectedIndex = 0;
                dtmBirthday.SelectedDate = null;
            }
            else
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin!", "Thông báo");
        }
	}
}